#!/bin/bash

update_type_list=( \
	                "rtfs" \
	                "root" \
	                "data" \
					)

main_proc()
{
	if [ $# -ne 2 ]; then
		return 2
	fi
	for t in ${update_type_list[@]}
	do
		if [ "x${t}" = "x$1" ]; then
			export dirname=update_${t}
			break
		fi
	done
	if [ -z "$dirname" ]; then
		echo "不支持的固件类型: $1"
		return 1
	fi

	if [ ! -d $2 ]; then
		echo "未找到文件夹: $2"
		return 1
	fi

	if [ "x$2" = "x${dirname}" ]; then
		echo "文件夹名称不能为: ${dirname}"
		return 1
	fi

	rm ${dirname} -rf > /dev/null 2>&1
	sudo cp -rf $2 ${dirname}
	sudo chown $(whoami):$(whoami) ${dirname}

	cd ${dirname}
	sudo chown root:root -R ./
	sudo tar czf ../${dirname}.tar.gz ./
	sudo chown $(whoami):$(whoami) -R ./
	cd -> /dev/null

	sudo chown $(whoami):$(whoami) ${dirname}
	sudo chown $(whoami):$(whoami) ${dirname}.tar.gz
	echo "生成压缩文件: ${dirname}.tar.gz ..."

	md5sum  ${dirname}.tar.gz | awk {'print $1'} >${dirname}.md5
	sudo chown $(whoami):$(whoami) ${dirname}.md5
	echo "计算校验文件: ${dirname}.md5 ..."

	rm ${dirname} -rf > /dev/null 2>&1

	if [ ! -d vend ]; then
		mkdir -p vend
	fi
	mv  ${dirname}.tar.gz  vend/
	mv  ${dirname}.md5     vend/
	echo "所需存储容量:"
	ls -sh vend/ | grep ${dirname}

}

echo "正在将obmdata,obmroot,obmrtfs打包为升级数据..."
main_proc data obmdata
main_proc root obmroot
main_proc rtfs obmrtfs
echo "已经将obmdata,obmroot,obmrtfs打包为升级数据!"
echo "输出目录为./vend"
ls ./vend/

